package HashBuck;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 苏李涛
 * Date: 2024-07-30
 * Time: 22:39
 */
public class MyHashBuck2<K,V> {
    static class Node<K,V> {
        public K key;
        public V val;
        public Node next;

        public Node(K key, V val) {
            this.key = key;
            this.val = val;
        }

    }

    public int usedSize;
    public Node<K,V>[] array = (Node<K,V>[])new Node[10];
    public static final double DEFAULT_LOAD_FACTOR = 0.75f;

    public void push(K key, V val) {
        int hashCode = key.hashCode();//转化为整数
        int index = hashCode % array.length;
       Node<K,V> cur = array[index];

       while (cur != null) {
           if (cur.key.equals(key)/*引用类型比较*/) {
               cur.val = val;
               return;
           }
           cur = cur.next;
       }

        //当前链表没有Key这个节点
        //头插：
        Node<K,V> node = new Node<>(key,val);
        node.next = array[index];
        array[index] = node;
        usedSize++;

    }

    public V getVal(int key) {
        int index = key % array.length;
        Node<K,V> cur = array[index];

        while (cur != null) {

            //不能有相同的节点
            if (cur.key.equals(key)) {
                return cur.val;
            }
            cur = cur.next;
        }
        return null;
    }



}
